package com.sh.web.controller;

import com.github.cage.Cage;
import com.github.cage.GCage;
import com.sh.web.model.ResponseResult;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ressecurity")
@Slf4j
public class CaptchaController {

    private final Cage cage = new GCage();

    // 应为验证码生成后存在session中，所以需要先获取到session
    @RequestMapping("/captcha")
    public String getCaptcha(HttpSession session) {
        String token = cage.getTokenGenerator().next();
        log.info("验证码为：{}", token);
        session.setAttribute("captcha", token);
        byte[] image = cage.draw(token);
        return Base64.getEncoder().encodeToString(image);
    }

    @PostMapping("/getTelValcode")
    public ResponseResult getTelValcode( @RequestBody String tel, HttpSession session ) {
        System.out.println(tel);
        // 调用业务层发送验证码
        // 业务层调用jms发送验证码
        // 远程调用  拿到返回的验证码
        session.setAttribute("telValcode", "1234");
        return ResponseResult.ok().setData("1234");
    }


}
